<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
}

$dialogsResultSet = getDialogs();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Диалоги</title>
    <link rel="stylesheet" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/dialogs.css">
    <script src="resources/jquery.js"></script>
    <meta charset="UTF-8">
</head>

<body class="body">
<?php
include_once("menu.php");
?>

<div id="square">
    <div class="dialogdiv">
        <div class="dialogheader">
            Ваши диалоги:
        </div>
        <form action="search_users.php" accept-charset="UTF-8" method="post" id="userform">
            <div>
                Поиск пользователя: <input type="text" maxlength="20" name="user" id="textarea"/>

                <div class="searchbutton" onclick="$('#userform').submit()">
                    Поиск
                </div>
            </div>
        </form>
        <?php

        while ($dialog = mysql_fetch_assoc($dialogsResultSet)) {
            $lastMsgInfoSet = getDialogLastMsgInfo($dialog['id']);

            $lastMessageInfo = mysql_fetch_assoc($lastMsgInfoSet);

            $msg = countDialogMessages($dialog['id']);

            $light = "box-shadow: 0 0 15px rgba(0,0,0,1)";

            if ($dialog['uread'] == 0 && $dialog['getter'] == $_SESSION['id']) {
                $light = "box-shadow: 0 0 15px rgba( 255, 255, 255, 1)";
            }
            ?>

            <div class="dialogcontentdiv">
                <div class="deletedialogbutton" onclick="$('#deletediv<?= $dialog['id'] ?>').slideDown('slow')">
                    x
                </div>
                <div class="dialog" style="<?= $light ?>"
                     onclick="location.href='conversation.php?did=<?= $dialog['id'] ?>'">
                    <div class="dialogcontentheader">
                        <?= $dialog['user'] ?>
                    </div>
                    <div class="sectioninfo">
                        <div class="sectioninfodiv">
                            Дата: <?= $lastMessageInfo['date'] ?>
                        </div>
                        <div class="sectioninfodiv">
                            Время: <?= $lastMessageInfo['time'] ?>
                        </div>
                        <div class="sectioninfodiv">
                            Сообщений: <?= $msg ?>
                        </div>
                    </div>
                </div>

                <div class="deletedialogdiv" id="deletediv<?= $dialog['id'] ?>">
                    Вы уверены, что хотите удалить данный диалог ?
                    (После удаления восстановить его будет невозможно)
                    <div class="askdeletediv">
                        <div class="deletebutton"
                             onclick="location.href='dialog_delete_handler.php?did=<?= $dialog['id'] ?>'">
                            Да
                        </div>
                        <div class="cancelbutton" onclick="$('#deletediv<?= $dialog['id'] ?>').slideUp('slow')">
                            Отмена
                        </div>
                    </div>
                </div>
            </div>
        <?php
        }
        ?>

    </div>
</div>

<?php
include_once("navigation.html");
?>
</body>
</html>
